import requests
import base64
from Crypto.Cipher import AES
import json
import time

class Qutuo:
    def __init__(self):
        self.access_token = ""
        self.aes = AES.new(str.encode("Qutuo@Client2018"), AES.MODE_ECB)
        #ahuSecond0425..?
        #default.pad.Pkcs7
        self.session = requests.Session()
        self.session.headers.update({
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36 Edg/81.0.416.58",
            "Content-Type": "application/x-www-form-urlencoded",
        })
        pass
    def UpdateHeader(self):
        self.session.headers.update({"authorization": self.Encrypt(json.dumps({"token":self.access_token,"platform":3,"version":"2.0.5","device":"FAAE6E21-013F-42D1-B722-599DE3DC340F","timestamp":int(round(time.time()) * 1000)}))})
    def Decrypt(self,data):
        decrypted_text = self.aes.decrypt(base64.decodebytes(bytes(data, encoding='utf8'))).decode("utf8")
        decrypted_text = decrypted_text[:-ord(decrypted_text[-1])]
        return decrypted_text
    def Encrypt(self,data):
        while len(data) % 16 != 0:
            data += (16 - len(data) % 16) * chr(16 - len(data) % 16)
        data = str.encode(data)
        return str(base64.encodebytes(self.aes.encrypt(data)), encoding='utf8').replace('\n', '')
    def Get(self,url,params=None):
        self.UpdateHeader()
        if params != None:
            url += "?params=" + self.Encrypt(json.dumps(params))
        return json.loads(self.Decrypt(self.session.get(url).text))
    def Post(self,url,params):
        self.UpdateHeader()
        return json.loads(self.Decrypt(self.session.post(url,"params="+self.Encrypt(json.dumps(params))).text))
    def Login(self,account,password,schoolCode="10361"):
        body = {"schoolCode":schoolCode,"code":account,"password":password}
        url = "https://stuapi.qutuo.net/api/app/client/v1/token"
        ret = self.Get(url,body)
        if ret["code"] == 0:
            self.access_token = ret["data"]["access_token"]
            print(self.access_token)
            return True 
        else: 
            print(ret["msg"])
            return False
    def GetUnReadCount(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/message/notice/un-read-count"
        ret = self.Get(url)
        return ret["data"]["unReadCount"]
    def GetMyInfo(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/student/user/my-info"
        ret = self.Get(url)
        return ret["data"]
    def GetDynamicUnReadCount(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/dynamic/unread/count"
        ret = self.Get(url)
        return ret["data"]["unReadCount"]
    def WXSignature(self):
        body = {"url":"https://stu.2ketang.com/login"}
        url = "https://stuapi.qutuo.net/api/app/client/v1/wx/signature"
        ret = self.Get(url,body)
        return ret["data"]
    def GetOrgList(self,pageSize=10,pageNum=1):
        url = "https://stuapi.qutuo.net/api/app/client/v1/organization/page/organization/list"
        body = {"pageSize":pageSize,"pageNum":pageNum,"sortType":1}
        ret = self.Get(url,body)
        return ret["data"]
    def GetModules(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/modules"
        ret = self.Get(url)
        return ret["data"]
    def GetBanners(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/banners"
        body = {"type":1}
        ret = self.Get(url,body)
        return ret["data"]
    def GetMyList(self,pageNum=1,pageSize=20):
        url = "https://stuapi.qutuo.net/api/app/client/v1/activity/my-list"
        body = {"pageNum":pageNum,"pageSize":pageSize,"status":6}
        ret = self.Get(url,body)
        return ret["data"]
    def GetMyInfo2(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/student/user/my/info"
        ret = self.Get(url)
        return ret["data"]
    def GetAmount(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/student/user/activity/amount"
        ret = self.Get(url)
        return ret["data"]
    def GetNationList(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/student/get/nation_list"
        ret = self.Get(url)
        return ret["data"]
    def GetAssumeRole(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/common/aliyun/sts/assume-role"
        ret = self.Get(url)
        return ret["data"]
        #MAYBE CAN UPLOAD ANYTHING?
    def UpdateInfo(self,headurl):
        url = "https://stuapi.qutuo.net/api/app/client/v1/student/user/update-info"
        body = {"avatar":headurl}
        ret = self.Post(url,body)
        return ret["data"]
    def Unknow(self):
        url = "https://stuapi.qutuo.net/api/app/client/v1/activity/processing/statistics"
        url = "https://stuapi.qutuo.net/api/app/client/v1/announcement/list/noPage"

if __name__ == "__main__":
    client = Qutuo()
    if client.Login("你的学号","你的密码"):
        print(client.GetUnReadCount())
        print(client.GetMyInfo())
        print(client.GetDynamicUnReadCount())
        print(client.WXSignature())
        print(client.GetOrgList())
        print(client.GetModules())
        print(client.GetBanners())
        print(client.GetMyList())
        print(client.GetNationList())
        print(client.GetAssumeRole())
        #print(client.UpdateInfo(""))
